package com.paomi.client.activity.person.presenter;

import android.app.Activity;
import android.content.DialogInterface;
import android.widget.TextView;

import com.hannesdorfmann.mosby3.mvp.MvpBasePresenter;
import com.hannesdorfmann.mosby3.mvp.MvpPresenter;
import com.hannesdorfmann.mosby3.mvp.MvpView;
import com.luck.picture.lib.PictureSelector;
import com.luck.picture.lib.compress.Luban;
import com.luck.picture.lib.config.PictureConfig;
import com.luck.picture.lib.config.PictureMimeType;
import com.paomi.client.activity.person.View.PersonMsgView;
import com.paomi.client.activity.person.model.UploadBean;
import com.paomi.client.base.BaseLoadMorePresenter;
import com.paomi.client.base.BaseMvpLCEView;
import com.paomi.client.bean.AddressListInfo;
import com.paomi.client.bean.BaseJSON;
import com.paomi.client.net.Api;
import com.paomi.client.net.BaseCallback;
import com.paomi.client.net.Constants;
import com.paomi.client.utils.Datas;
import com.paomi.client.utils.DeviceUtils;
import com.paomi.client.view.LoadingDialog;
import com.paomi.client.view.wheel.dialog.ChangeDateDialog;

import java.io.File;
import java.util.Map;

import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;
import retrofit2.Call;

public class PersonMsgPre extends MvpBasePresenter<PersonMsgView> {

    ChangeDateDialog.Builder dialog;//选择日期的滑轮

    /**
     * 选择出生年月
     *
     * @param
     */
    public void selectDate(Activity activity) {
        dialog = new ChangeDateDialog.Builder(activity);
        dialog.setDate(Datas.getDateYear(), Datas.getTimesYue(), Datas.getRiQi());
        dialog.setBirthdayListener(new ChangeDateDialog.Builder.OnBirthListener() {
            @Override
            public void onClick(String year, String month, String day) {
                StringBuilder sb = new StringBuilder();
                sb.append(year.substring(0, year.length() - 1)).append("-").append(month.substring(0, day.length() - 1)).append("-").append(day);
                if (year.contains("年")) {
                    year = year.substring(0, year.length() - 1);
                }
                if (month.contains("月")) {
                    month = month.substring(0, month.length() - 1);
                }
                if (day.contains("日")) {
                    day = day.substring(0, day.length() - 1);
                }
                getView().showBirthday(year + "-" + month + "-" + day);
            }
        });
        dialog.setClickListenerCancel(new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
            }
        });
        dialog.create().show();
    }


    /**
     * 跳转选择相册图片
     */
    public void getImageFromCamera(Activity activity) {
        PictureSelector.create(activity)
                .openGallery(PictureMimeType.ofImage())// 全部.PictureMimeType.ofAll()、图片.ofImage()、视频.ofVideo()、音频.ofAudio()
                .isCamera(false)// 是否显示拍照按钮 true or false
                .minSelectNum(1)// 最小选择数量
                .maxSelectNum(1)//最大选择数量
                .selectionMode(PictureConfig.MULTIPLE)// 多选 or 单选PictureConfig.SINGLE
                .enableCrop(true)// 是否裁剪 true or false
                .compress(true)// 是否压缩 true or false
                .previewImage(true)// 是否可预览图片
                .isZoomAnim(true)// 图片列表点击 缩放效果 默认true
                .freeStyleCropEnabled(false)// 裁剪框是否可拖拽 true or false
                .circleDimmedLayer(true)// 是否圆形裁剪 true or false
                .showCropFrame(false)// 是否显示裁剪矩形边框 圆形裁剪时建议设为false   true or false
                .showCropGrid(false)// 是否显示裁剪矩形网格 圆形裁剪时建议设为false    true or false
//                .compressMaxKB(215)
//                .glideOverride(DeviceUtils.dipToPX(112), DeviceUtils.dipToPX(74))// glide 加载宽高，越小图片列表越流畅，但会影响列表图片浏览的清晰度
                .forResult(PictureConfig.CHOOSE_REQUEST);//结果回调onActivityResult code
    }

    /**
     * 直接拍照
     *
     * @param activity
     */
    public void getCameraImage(Activity activity) {
        PictureSelector.create(activity)
                .openCamera(PictureMimeType.ofImage())
                .enableCrop(true)// 是否裁剪 true or false
                .compress(true)// 是否压缩 true or false
                .previewImage(true)// 是否可预览图片
                .isZoomAnim(true)// 图片列表点击 缩放效果 默认true
                .freeStyleCropEnabled(false)// 裁剪框是否可拖拽 true or false
                .circleDimmedLayer(true)// 是否圆形裁剪 true or
                .showCropFrame(false)// 是否显示裁剪矩形边框 圆形裁剪时建议设为false   true or false
                .showCropGrid(false)// 是否显示裁剪矩形网格 圆形裁剪时建议设为false    true or false
                .forResult(PictureConfig.CHOOSE_REQUEST);
    }


    /**
     * 设置用户信息
     */
    public void setUserInfo(Map<String, String> map) {
        //第一种网络请求方式
        Api.getInstanceService(Constants.User_URL).userInfo(map).enqueue(new BaseCallback<BaseJSON>() {
            @Override
            public void onResponse(Call<BaseJSON> call, BaseJSON result) {
                if(result!=null){
                    getView().showChangeUserInfo(result);
                }
            }
        });
    }

    /**
     * 获取地址总数
     */
    public void getAddressNum(Map<String, String> map) {
        Api.getInstanceService(Constants.User_URL).getAddressList(map).enqueue(new BaseCallback<AddressListInfo>() {
            @Override
            public void onResponse(Call<AddressListInfo> call, AddressListInfo result) {
                if(isViewAttached()){
                    getView().showAddressNum(result);
                }
            }
        });
    }

    /**
     * 上传个人图像
     *
     * @param path
     */
    public void uploadMineImg(String path) {
        File file = new File(path);
        RequestBody fbody = RequestBody.create(MediaType.parse("image/*"), file);
        MultipartBody.Part no =
                MultipartBody.Part.createFormData("file", file.getName(), fbody);
        Api.getInstanceService(Constants.BASE_URL).uploadImg(no, file.getName()).enqueue(new BaseCallback<UploadBean>() {
            @Override
            public void onResponse(Call<UploadBean> call, UploadBean result) {
                getView().showHeadImgUrl(result.getReturnData().get(0));
            }
        });
    }

}
